/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PublicGoodProvisionStrategyComponent.java
 *
 * Created on 15/Dez/2011, 0:04:47
 */

package intopo.game.impl.pgp;

import intopo.game.gui.AbstractStrategyEditorComponent;

/**
 *
 * @author pedro
 */
public class PublicGoodProvisionStrategyComponent
	extends AbstractStrategyEditorComponent<PublicGoodProvisionStrategy>
{
	/**
	 * Bitmap that indicates if there is an error in the strategy parameter.
	 */
	private int error;
	/** Creates new form BeanForm */
	public PublicGoodProvisionStrategyComponent ()
	{
		this.strategy = new PublicGoodProvisionStrategy (1f);
		this.error = 0;
		initComponents();
	}
	@Override
	public String getConfigFileDescription ()
	{
		PublicGoodProvisionStrategy s = this.getStrategy ();
		if (s != null) {
			return String.format ("PublicGoodProvisionStrategy %d", s.getProvideProbability ());
		}
		else {
			return null;
		}
	}

	@Override
	public PublicGoodProvisionStrategy getStrategy ()
	{
		if (error == 0) {
			final float value = (Float) this.provisionProbabilityFormattedTextField.getValue ();
			this.strategy = new PublicGoodProvisionStrategy (value);
			return this.strategy;
		}
		else {
			return null;
		}
	}

	@Override
	public void setStrategy (PublicGoodProvisionStrategy strategy)
	{
		this.strategy = strategy;
		this.provisionProbabilityFormattedTextField.setValue(new Float (this.strategy.getProvideProbability ()));
		this.binStrategyFormattedTextField.setValue (new Integer (strategy.getBin ()));
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
      provisionProbabilityFormattedTextField = new javax.swing.JFormattedTextField();
      separator = new javax.swing.JSeparator();
      javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
      binStrategyFormattedTextField = new javax.swing.JFormattedTextField();

      setLayout(new java.awt.GridBagLayout());

      jLabel1.setText("Provision probability: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      add(jLabel1, gridBagConstraints);

      provisionProbabilityFormattedTextField.setColumns(5);
      provisionProbabilityFormattedTextField.setValue(new Float (this.strategy.getProvideProbability ()));
      provisionProbabilityFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            provisionProbabilityFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      add(provisionProbabilityFormattedTextField, gridBagConstraints);
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridwidth = 2;
      gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
      add(separator, gridBagConstraints);

      jLabel2.setText("Bin: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      add(jLabel2, gridBagConstraints);

      binStrategyFormattedTextField.setColumns(5);
      binStrategyFormattedTextField.setEnabled(false);
      binStrategyFormattedTextField.setValue(new Integer (this.strategy.getBin ()));
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      add(binStrategyFormattedTextField, gridBagConstraints);
   }// </editor-fold>//GEN-END:initComponents

	 private void provisionProbabilityFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_provisionProbabilityFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.provisionProbabilityFormattedTextField.getValue ();
			final boolean ok = value >= 0 && value <= 1;
			if (ok) {
				error |= ~1;
			}
			else {
				error &= 1;
			}
			this.update (this.provisionProbabilityFormattedTextField, ok);
		}
	 }//GEN-LAST:event_provisionProbabilityFormattedTextFieldPropertyChange


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JFormattedTextField binStrategyFormattedTextField;
   private javax.swing.JFormattedTextField provisionProbabilityFormattedTextField;
   private javax.swing.JSeparator separator;
   // End of variables declaration//GEN-END:variables

}
